package com.navi.loaddata.job;

import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.job.QuartzManage;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadService.IEtlConfServices;
import com.navi.loaddata.loadService.IEtlConfServices2;
import com.navi.loaddata.loadService.impl.arrayCfMappingData.LoadArrayCfMappingDataService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author jay
 */
@Service
//@DisallowConcurrentExecution//禁止并发
public class LoadArrayCfMappingData implements Job {
    private static final Logger logger = LoggerFactory.getLogger(LoadArrayCfMappingData.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private QuartzManage quartzManage;
    @Autowired
    private LoadArrayCfMappingDataService loadArrayCfMappingDataService;
    @Autowired
    private IEtlConfServices2 etlConfServices;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //get JOB
        EtlConfD etlConfD = etlConfServices.getNewConf(context);
        if (null == etlConfD) return;
        etlConfD = etlConfServices.beginJob(etlConfD);
        EtlConfD ashtOpeHis=etlConfServices.getNewConf("ashtopeHis");
        EtlConfD fshtOpeHis=etlConfServices.getNewConf("fshtopeHis");
        EtlConfD cshtOpeHis=etlConfServices.getNewConf("cshtopeHis");
        EtlConfD opeInfo=etlConfServices.getNewConf("opeInfo");
        if (StringUtils.isEmpty(etlConfD.getQueryMinute())) return;
        if(etlConfD.getRunEndTimestamp().after(ashtOpeHis.getRunStartTimestamp())||etlConfD.getRunEndTimestamp().after(fshtOpeHis.getRunStartTimestamp())||etlConfD.getRunEndTimestamp().after(cshtOpeHis.getRunStartTimestamp())||etlConfD.getRunEndTimestamp().after(opeInfo.getRunStartTimestamp())){
            etlConfD.setRunEndTimestamp(null);
            etlConfD.setRunFlg("N");
            etlConfServices.saveReloadJob(etlConfD);
            return;
        }
        startETL(etlConfD);
    }

    //全站点跑
    private void startETL(EtlConfD etlConfD) {
        String[] resMsg = new String[3];
        try {
            resMsg = loadArrayCfMappingDataService.loadArrayCfMappingDataMain(etlConfD);
            EtlConfDModel etlConfDByOpe = etlConfServices.endJobOfModel(etlConfD);
            if (null == etlConfDByOpe) return;
            etlConfServices.addHisOfModel(etlConfDByOpe, resMsg);
        } catch (ETLException2 e) {
            String err = e.getErrMsg();
            //print Log
            logger.error("Job {} [{}]-[{}] FAIL TO SYNCHRONIZE.\n{}\n{}",
                    etlConfD.getProjectName() + "_" + etlConfD.getJobGroupName() + "_" + etlConfD.getJobName(),
                    etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp(),
                    e.getErrorCode().getErrorMsg(), LoggerUtil.stackTraceToString(e)
            );
            EtlConfDModel etlConfDByOpe = etlConfServices.endJobOfModel(etlConfD);
            if (null == etlConfDByOpe) return;
            resMsg[0] = "0";
            resMsg[1] = "-1";
            resMsg[2] = err;
            etlConfServices.addHis(etlConfD, resMsg);

            if (99999 == e.getErrorCode().getErrorCode()) {//无法连接,SLEEP
                etlConfServices.resetJob(etlConfD);
                return;
            }
        }catch (Exception e){
            String err = LoggerUtil.stackTraceToString(e);
            logger.error("Job {} [{}]-[{}] FAIL TO SYNCHRONIZE.\n{}",
                    etlConfD.getProjectName() + "_" + etlConfD.getJobGroupName() + "_" + etlConfD.getJobName(),
                    etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp(),
                    LoggerUtil.stackTraceToString(e)
            );
            EtlConfDModel etlConfDByOpe = etlConfServices.endJobOfModel(etlConfD);
            if (null == etlConfDByOpe) return;
            resMsg[0] = "0";
            resMsg[1] = "-1";
            resMsg[2] =err;
            etlConfServices.addHisOfModel(etlConfDByOpe, resMsg);

            if (null != e && "Y".equals(etlConfServices.specialErr(etlConfDByOpe.getJobName(),err))) {//无法连接,SLEEP
                etlConfServices.resetJob(etlConfD);
                return;
            }
        }
        etlConfServices.endJob(etlConfD);
    }
}
